BEGIN { printline = 1; startingproc = 0 }

$0 ~ /^$/ {next;}

procnamenext == 1 {
    procs[startingproc] = $1;
    procnamenext = 0;
    printline = 0;
    }
$1 == "procedure" || $1 == "function" {
    startingproc++;
    procnamenext = 1;
    printline = 0;
    }
$1 == "forward" {
    startingproc--;
    }
$1 == "begin" {
    if (blockmarks++ == 0)
	{
	if (startingproc > 0)
	    printf("DEFUN\n%s",procs[startingproc--]);
	else
	    printf("DEFUN\n_main");
	printline = 0;
	}
    }

{ if ( printline == 1 )
    printf("%s",$1);
  else
    printline = 1;
}

$1 == "end" {
    if (--blockmarks == 0 && ! inrecord)
	printf("\nEND_DEFUN\n");
    inrecord = 0;
    }
$1 == "case" {
    if ( ! inrecord )
	blockmarks++;
    }
$1 == "record" {
    if ( ! inrecord )
	blockmarks++;
    inrecord  = 1;
    }
{printf("\n");}
